package bancosys.tec.utils.db.executor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author frank
 */
public class HsqlDbExecutor extends JdbcDbExecutor {

    /**
     * @return o char delimitador
     */
    @Override
    public char getDelimiterChar() {
        return ';';
    }

    private static final Pattern CREATE_VIEW_PATTERN = Pattern.compile("CREATE VIEW (.*) AS.*");;

    private static final Pattern CREATE_SEQ_PATTERN = Pattern.compile("CREATE SEQUENCE (SEQ_[^ ]*).*");

    /**
     * converte um statement se necessário
     * 
     * @param statement o statement
     * @return o statement modificado
     */
    @Override
    public String convertStatement(String statement) {
        Matcher m = CREATE_VIEW_PATTERN.matcher(statement.split("\n")[0]);
        if (m.matches()) {
            return statement.replaceFirst("CREATE VIEW ", "DROP VIEW " + m.group(1) + " IF EXISTS; CREATE VIEW ");
        }

        m = CREATE_SEQ_PATTERN.matcher(statement);
        if (m.matches()) {
            return statement.replaceFirst("CREATE SEQUENCE ", "DROP SEQUENCE " + m.group(1) + " IF EXISTS; CREATE SEQUENCE ");
        }

        return statement;
    }
}